package com.icss.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import com.icss.entity.Address;
import com.icss.entity.Cart;
import com.icss.entity.CartBean;
import com.icss.entity.Custom;
import com.icss.entity.Order;
import com.icss.entity.OrderBean;
import com.icss.entity.OrderMessageBean;

public interface OrderDao {

	/*查找部分*/

	//显示全部订单
	@Select("select Oid,corder.OSid,OSname,c_name,address_phone,address_name,address_detail,SOtime,GOtime,Oamount from corder,custom,address,orderstate where corder.OSid=orderstate.OSid and corder.address_id=address.address_id and custom.c_id=address.c_id and corder.c_id=#{c_id};")
	public List<OrderBean> ShowAllOrders(Integer c_id);

	//通过状态查找订单
	@Select("select Oid,corder.OSid,OSname,c_name,address_phone,address_name,address_detail,SOtime,GOtime,Oamount from corder,custom,address,orderstate where corder.OSid=orderstate.OSid and corder.address_id=address.address_id and custom.c_id=address.c_id and corder.c_id=#{0} and corder.OSid=#{1};")
	public List<OrderBean> ShowOrderByState(Integer c_id,Integer OSid);

	//显示订单详情
	@Select("select OMid,DishName,DishPicture,DishPrice,Mnumber,Oid from ordermessage,menu where menu.DishId=ordermessage.DishId and Oid=#{Oid};")
	public List<OrderMessageBean> ShowOrderMessage(Integer Oid);

	//用户账号测试调用
	@Select("select * from custom where c_id=#{c_id};")
	public Custom loginJudgement(Integer c_id);

	//显示订餐车内容
	@Select("select cart_id,DishName,DishPrice,DishPicture,dish_num,c_id from cart,menu where menu.DishId=cart.DishId and c_id=#{c_id};")
	public List<CartBean> ShowCart(Integer c_id);

	//显示订餐车测试
	@Select("select cart.*,DishPrice,DishPicture from cart,menu where menu.DishId=cart.DishId and c_id=#{c_id};")
	public List<Cart> useCart(Integer c_id);

	//显示新建订单
	@Select("select * from corder where c_id=#{0} and Oid=#{1};")
	public List<Order> ShowTest(Integer c_id,Integer Oid);

	//显示收货地址
	@Select("select * from address where c_id=#{c_id}")
	public List<Address> showAddress(Integer c_id);

	//显示添加地址测试
	@Select("select * from address where c_id=#{0} and address_id=#{1}")
	public List<Address> showAddresstest(Integer c_id,Integer address_id);

	//订单详情界面-显示订单、地址
	@Select("select * from corder,address where corder.address_id=address.address_id and Oid=#{Oid};")
	public List<OrderBean> OMorder(Integer Oid);

	//订单详情界面-显示详情
	@Select("select * from ordermessage,menu where ordermessage.DishId=menu.DishId and Oid=#{Oid};")
	public List<OrderMessageBean> OMmessage(Integer Oid);




	/*添加部分*/

	//添加订餐车
	@Insert("insert into cart (cart_id,c_id,DishId,dish_num) values(#{1},#{0},#{2},1);")
	public int insertCart(Integer c_id,Integer cart_id,Integer DishId);

	//添加订单详情
	@Insert("insert into ordermessage (OMid,DishId,Oid,Mnumber) values(#{0},#{1},#{2},#{3});")
	public int insertOrderMessage(Integer cart_id,Integer DishId,Integer Oid,Integer dish_num);

	//添加收货地址
	@Insert("insert into address (address_id,c_id,address_name,address_phone,address_detail) values(#{address_id},#{c_id},#{address_name},#{address_phone},#{address_detail});") 
	public int insertAddress(Address address);

	//添加订单
	@Insert("insert into corder (Oid,c_id,OSid,Oamount) values(#{Oid},#{c_id},1,#{Oamount});") 
	public int insertOrder(Order order);

	/*修改部分*/

	//修改餐品数量
	@Update("update cart set dish_num=#{1} where cart_id=#{0};")
	public int editDNum(Integer cart_id,Integer dish_num);

	//修改地址信息
	@Update("update address set address_name=#{1},address_phone=#{2},address_detail=#{3} where address_id=#{0};")
	public int editAddress(Integer address_id,String address_name,Integer address_phone,String address_detail);

	//修改订单
	@Update("update corder set SOtime=now(),OSid=2,address_id=#{1} where Oid=#{0};")
	public int editOrder(Integer Oid,Integer address_id);

	//修改订单留言
	@Update("update corder set message=#{1} where Oid=#{0};")
	public int editMessage(Integer Oid,String message);






	/*删除部分*/

	//删除订餐车中信息
	@Delete("delete from cart where cart_id=#{cart_id}")
	public int DeleteCart(Integer cart_id);

	//删除地址
	@Delete("delete from address where address_id=#{address_id}")
	public int DeleteAddress(Integer address_id);





	/*增加部分*/

	@SelectProvider(type=OrderPriv.class,method="selectOrder")
	public List<OrderBean> findAll(Order order);

	@Update("update corder set Oamount=(SELECT SUM(ordermessage.Mnumber * menu.DishPrice) from ordermessage,menu where ordermessage.DishId=menu.DishId AND ordermessage.Oid=#{1}) where Oid=#{0}")
	public int updateOa(int Oid,int oid);

	@Select("select * from corder where Oid=#{Oid}")
	public Order findByOid(Integer Oid);

	@Update("update corder set OSid=#{0} where Oid=#{1}")
	public int upOs(int OSid,int Oid);

	@Select("select corder.*,address.address_name,address.address_detail,address.address_phone from corder,address where corder.address_id=address.address_id and Oid=#{Oid}")
	public OrderBean findAddByOid(Integer Oid);

	@Delete("delete from corder where Oid=#{Oid}")
	public int DeleteCord(Integer Oid);

}
